<?php
/**
 * (jobs) (JP)备货品类/sku预估
 */
namespace App\Jobs\Stocking;

use App\Models\Basics\SysArea;
use App\Models\Basics\SysPlat;
use App\Models\Product\ProductSku;
use App\Models\SsBasic\AreaClassifyEstimate;
use App\Models\SsBasic\AreaSkuEstimate;
use App\Models\SsBasic\JpClassifyEstimate;
use App\Models\SsBasic\JpSkuEstimate;
use App\Models\SsBasic\JpStockingSku;
use App\Models\SsBasic\JpStockingSkuRatio;
use App\Models\Stocking\StockingSku;
use App\Models\Stocking\StockingSkuRatio;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class JpEstimateJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $estimates;

    /**
     * Estimate constructor.
     * @param $estimates
     */
    public function __construct($estimates)
    {
        $this->estimates = $estimates;
    }

    /**
     * Execute the job.
     * @return void
     */
    public function handle()
    {
        $this->exeFillData($this->estimates);
    }

    private function exeFillData($estimates){

        $areaId = SysArea::AREA_JP; //JP站点
        $classifyKey = [];
        $skuKey = [];

        //1. 根据比例，将JP品类预估分解到JPsku预估，同步更新JPsku备货表里的预估
        //更新,JPsku预估、JP备货sku及相关信息
        foreach ($estimates as $estimate){

            $classifyId = $estimate->classify_id;
            if(!in_array($classifyId,$classifyKey)){
                $classifyKey[] = $classifyId;
            }

            //备货sku(JP)
            $jpStockingSkus = JpStockingSku::query()
                ->where('plat_id',$estimate->plat_id)
                ->whereHas('sku',function ($query) use ($classifyId){
                    $query->where('classify_id',$classifyId);
                })->get();
            foreach ($jpStockingSkus as $jpStockingSku){

                $key = $jpStockingSku->plat_id.'_'.$jpStockingSku->psku_id;
                if(!in_array($jpStockingSku->psku_id,$skuKey)){
                    $skuKey[] = $jpStockingSku->psku_id;
                }

                //sku销售比例(JP)
                $ratio = JpStockingSkuRatio::getRatio($key);

                //sku预估(JP)
                $jpSkuEstimate = JpSkuEstimate::query()->firstOrNew(['area_psku' => $key]);
                for($i = 0;$i < JpSkuEstimate::ESTIMATE_NUM; $i++){
                    $estimateNum = intval(intval($estimate->{'estimate_num'.$i}) * $ratio);
                    $jpStockingSku->{'estimate_num'.$i} = $estimateNum;
                    $jpSkuEstimate->{'estimate_num'.$i} = $estimateNum;
                }
                $jpStockingSku->save();
                $jpSkuEstimate->save();

                $jpStockingSku->over_stock = JpStockingSku::getOverStock($jpStockingSku);
                $jpStockingSku->over_stock_value = JpStockingSku::getOverStockValue($jpStockingSku->over_stock,$jpStockingSku->unit_price);
                $jpStockingSku->save();
            }
        }
        sleep(10);


        /********更新JP站点 start***********/
        //更新,JP_品类
        foreach ($classifyKey as $_classifyId){

            //JP平台品类预估汇总
            $classifyEstimateSum = JpClassifyEstimate::query()->selectRaw(getQueryEstimateStr())->where('classify_id' ,$_classifyId)->first();

            //更新JP站点品类预估 (备货站点品类总表)
            $areaClassifyEstimate = AreaClassifyEstimate::query()->firstOrNew(['area_id' => $areaId, 'classify_id' => $_classifyId]);
            for($i = 0; $i < AreaSkuEstimate::ESTIMATE_NUM; $i++){
                $areaClassifyEstimate->{'estimate_num'.$i} = intval($classifyEstimateSum->{'estimate_num'.$i});
            }
            $areaClassifyEstimate->save();
        }

        //更新,JP_产品
        foreach ($skuKey as $_skuId){
            $classifyId = ProductSku::getValueById($_skuId, 'classify_id');
            if(!$classifyId) continue;

            $areaPsku = $areaId.'_'.$_skuId; //唯一键标识(JP)

            //JP备货平台(amazon、rakuten)
            $platPskus = [];
            foreach (SysPlat::$_jpStockPlatformId as $platId){
                $platPskus[] = $platId.'_'.$_skuId;
            }

            //JPsku预估 (JP平台sku预估汇总 === JP站点sku预估总量)
            $jpSkuEstimateSum = JpSkuEstimate::query()
                ->selectRaw(getQueryEstimateStr())
                ->whereIn('area_psku', $platPskus)
                ->first();

            //JP站点品类预估(备货站点品类总表)
            $areaClassifyEstimate = AreaClassifyEstimate::query()->where(['area_id' => $areaId, 'classify_id' => $classifyId])->first();
            if(!$areaClassifyEstimate) continue;

            //JP站点SKU预估(备货站点sku总表)
            $areaSkuEstimate = AreaSkuEstimate::query()->firstOrNew(['area_psku'=> $areaPsku]);

            //JPsku预估 (JP平台sku预估汇总 === JP站点sku预估总量)
            //JP站点备货sku(备货总表)
            $areaStockingSku = StockingSku::query()->where('area_psku',$areaPsku)->first();
            for($i = 0; $i < AreaSkuEstimate::ESTIMATE_NUM; $i++){
                $areaSkuEstimate->{'estimate_num'.$i} = intval($jpSkuEstimateSum->{'estimate_num'.$i}) ?? 0;
                $areaStockingSku->{'estimate_num'.$i} = intval($jpSkuEstimateSum->{'estimate_num'.$i}) ?? 0;
            }
            $areaSkuEstimate->save();

            $areaStockingSku->area_id = $areaId;
            $areaStockingSku->psku_id = $_skuId;
            $areaStockingSku->classify_id = $classifyId;
            $areaStockingSku->save();
            $areaStockingSku->over_stock = StockingSku::getOverStock($areaStockingSku);
            $areaStockingSku->over_stock_value = StockingSku::getOverStockValue($areaStockingSku->over_stock, $areaStockingSku->unit_price);
            $areaStockingSku->save();

            //2.4 更新JP站点sku销售比例 (依据JP站点sku预估与JP站点品类预估,计算JP站点sku比例)
            $skuRatio = StockingSkuRatio::query()->firstOrNew(['area_psku'=> $areaPsku]);
            $classifyEstimateNum = 0;
            $skuEstimateNum = 0;
            for($i = 0; $i < AreaSkuEstimate::ESTIMATE_NUM; $i++){
                $classifyEstimateNum = intval($areaClassifyEstimate->{'estimate_num'.$i}) ?? 0; //指定参数(JP站点的品类预估数量)
                $skuEstimateNum = intval($areaSkuEstimate->{'estimate_num'.$i}) ?? 0; //指定参数(JP站点的sku预估数量)
                if($classifyEstimateNum != 0 && $skuEstimateNum != 0){
                    break;
                }
            }
            if($classifyEstimateNum != 0 && $skuEstimateNum != 0){
                $stockingSkuRatio = sprintf("%.4f", $skuEstimateNum / $classifyEstimateNum);
                $skuRatio->ratio = $stockingSkuRatio; //0.3001
                $skuRatio->save();
            }
        }
        /********更新JP站点 end**********/
    }

}
